package com.cop4331.social;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

/**
 * 
 * @author Matt Szoke
 * @LastUpdated: 4/12/2012 by Matt Szoke
 * 
 * Reply to received message
 * 
 */
public class ReplyMessageActivity extends Activity {

	EditText subject;
	EditText to;
	EditText message;
	TextView err;
	Button cancel;
	Button send;

	// -----------------------------------------------
	// ONCREATE
	// -----------------------------------------------
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		// XML associated with SendMessage
		setContentView(R.layout.sendmessage);

		// TextViews
		err = (TextView) findViewById(R.id.err);

		// EditText
		subject = (EditText) findViewById(R.id.et_sub);
		to = (EditText) findViewById(R.id.et_to);
		message = (EditText) findViewById(R.id.et_message);

		// Button
		cancel = (Button) findViewById(R.id.b_cancel);
		send = (Button) findViewById(R.id.b_send);

		// Get Current User information from Database
		DatabaseHandler db = new DatabaseHandler(getApplicationContext());
		HashMap<String, String> user = db.getUserDetails();
		final String un = user.get("name");

		// Retreive bundle from previous activity
		// Bundle contains tutor selected in ChooseTutorActivity
		Bundle b = getIntent().getExtras();
		final String sender = b.getString("sender");
		final String sub = b.getString("subject");

		to.setText(sender);
		
		// We are replying, set subject with RE:
		if (sub.compareTo("null") != 0)
			subject.setText("RE: " + sub);
		
		// Adds functionality to send button
		send.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				// Retreives username from EditText Field
				String username = to.getText().toString();
				String subIn = subject.getText().toString();
				String mess = message.getText().toString();

				// Check that user exists
				UserFunctions userFunction = new UserFunctions();
				JSONObject json = userFunction.userExists(username);

				try {
					if (json.getString("success") != null) {

						String res = json.getString("success");

						// If user exists
						if (Integer.parseInt(res) == 1) {
							
							// Check strings, make sure they're filled in
							if (username.compareTo("") == 0)
								err.setText("Please enter a user.");

							else if (subIn.compareTo("") == 0)
								err.setText("Please enter a subject");

							else if (mess.compareTo("") == 0)
								err.setText("Please enter a message");
							
							// Add message to database
							else {
								userFunction.addMessage(un, username, subIn,
										mess);

								finish();
							}

						}
						// Username entered does not exist
						else
							err.setText("User does not exist.");
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		});

		// Gives functionality to "CANCEL" button
		cancel.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				finish();

			}
		});
		
		// Title Bar Buttons
		ImageButton about, home, message;

		about = (ImageButton) findViewById(R.id.b_about);
		home = (ImageButton) findViewById(R.id.b_home);
		message = (ImageButton) findViewById(R.id.b_message);

		UserFunctions uf = new UserFunctions();
		
		// Check for new messages
		JSONObject j_mess = uf.retreiveMessages(un, "new");

		try {
			if (Integer.parseInt(j_mess.getString("size")) > 0) {
				message.setVisibility(View.VISIBLE);
			} else
				message.setVisibility(View.GONE);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}
	
		// Show app info
		about.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				Dialog dialog = new Dialog(ReplyMessageActivity.this);

				dialog.setContentView(R.layout.help_dialog);
				dialog.setTitle("CramSessions v 1.0 2012");
				dialog.setCancelable(true);

				// Create and show a dialog to help the user with this specific
				// field
				TextView text = (TextView) dialog.findViewById(R.id.text);
				text.setText("CramSessions was created by:\n\n-Matt Szoke\n-Daniel Moody\n-Jeovanny Alvarez\n-Kia Manoochehri\n-Rj Anderson\n-Doug Wagner");
				text.setGravity(Gravity.LEFT);
				dialog.setCanceledOnTouchOutside(true);
				dialog.show();
			}
		});

		// Button to return to dashboard
		home.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				Intent sa = new Intent(getApplicationContext(),
						DashBoardActivity.class);
				sa.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(sa);
				finish();

			}
		});

		// Goto inbox if new message exists
		message.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				Intent sa = new Intent(getApplicationContext(),
						MessageNotifications.class);
				sa.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(sa);
				finish();

			}
		});
	}
}
